package com.ycu.jk16.mall.dao;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.CATEGORYTABLE;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.ycu.jk16.mall.domain.Category;

/**
 * 
 * @ClassName CategoryDao
 * @Description TODO(Category表的查询接口)
 * @author 倪宗龙
 * @date 2018年12月11日 下午1:16:37
 * @version V1.0
 */
public interface CategoryDao {
	// 以id查询子类
	@Select("select * from " + CATEGORYTABLE + " where id = #{id} ")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "parentcategory_id", property = "parentCategory", one = @One(select = "com.ycu.jk16.mall.dao.ParentCategoryDao.selectById", fetchType = FetchType.EAGER)) })
	Category selectById(@Param("id") Integer id);

	// 以id查询同级递归子类
	@Select("select * from " + CATEGORYTABLE + " where parentcategory_id = #{parentCategoryId} ")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "parentcategory_id", property = "parentCategory", one = @One(select = "com.ycu.jk16.mall.dao.ParentCategoryDao.selectById", fetchType = FetchType.EAGER)) })
	List<Category> selectAllById(@Param("parentCategoryId") Integer parentCategoryId);

	// 获取所有子类
	@Select("select * from " + CATEGORYTABLE)
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "parentcategory_id", property = "parentCategory", one = @One(select = "com.ycu.jk16.mall.dao.ParentCategoryDao.selectById", fetchType = FetchType.EAGER)) })
	List<Category> getAllCategory();

	// 查询子类总数
	@Select("select count(*) from " + CATEGORYTABLE)
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "parentcategory_id", property = "parentCategory", one = @One(select = "com.ycu.jk16.mall.dao.ParentCategoryDao.selectById", fetchType = FetchType.EAGER)) })
	Integer countCategory();

	// 增加子类
	@Insert({ "insert into " + CATEGORYTABLE
			+ "(parentcategory_id, name) values(#{parentcategoryId}, #{name})" })
	void addCategory(@Param("parentcategoryId") Integer parentcategoryId, @Param("name") String name);

	// 修改子类
	@Update({ "update " + CATEGORYTABLE
			+ " set parentcategory_id=#{category.parentCategory.id} , name=#{category.name} where id=#{category.id}" })
	void updateCategory(Category category);

	// 修改子类名字
	@Update({ "update " + CATEGORYTABLE + " set name=#{name} where id=#{id}" })
	void updateCategoryName(Category category);
}
